package com.example.fourtestapp.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.example.fourtestapp.R;
import com.example.fourtestapp.utils.ImageUtils;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ViolationFragment extends Fragment {

    private TabItem violationImg;
    private TabItem violationVideo;
    private GridView violationItemView;
    private TabLayout tabs;

    private RequestQueue queue;

    private List<String> imgUrl;
    private List<String> videoUrl;

    private mViolationAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_violation, container, false);

        queue = Volley.newRequestQueue(getContext());
        imgUrl = new ArrayList<>();
        imgUrl.add("http://liushenlong.cn:8099/liusl/images/1.jpg");
        imgUrl.add("http://liushenlong.cn:8099/liusl/images/2.jpg");
        imgUrl.add("http://liushenlong.cn:8099/liusl/images/3.jpg");
        imgUrl.add("http://liushenlong.cn:8099/liusl/images/4.jpg");
        imgUrl.add("http://liushenlong.cn:8099/liusl/images/5.jpg");
        imgUrl.add("http://liushenlong.cn:8099/liusl/images/6.jpg");
        imgUrl.add("http://liushenlong.cn:8099/liusl/images/7.jpg");
        imgUrl.add("http://liushenlong.cn:8099/liusl/images/8.jpg");

        videoUrl = new ArrayList<>();
        videoUrl.add("http://liushenlong.cn:8099/liusl/videos/wz_1.mp4");
        videoUrl.add("http://liushenlong.cn:8099/liusl/videos/wz_2.mp4");
        videoUrl.add("http://liushenlong.cn:8099/liusl/videos/wz_3.mp4");
        videoUrl.add("http://liushenlong.cn:8099/liusl/videos/wz_4.mp4");
        videoUrl.add("http://liushenlong.cn:8099/liusl/videos/wz_5.mp4");

        initView(view);
        initListener();

        adapter = new mViolationAdapter("违章图片");
        violationItemView.setAdapter(adapter);
        return view;
    }

    private void initView(View view) {
        violationImg = view.findViewById(R.id.violation_img);
        violationVideo = view.findViewById(R.id.violation_video);
        violationItemView = view.findViewById(R.id.violation_itemView);
        tabs = view.findViewById(R.id.tabs);
    }

    private void initListener() {
        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                adapter = new mViolationAdapter(tab.getText().toString());
                violationItemView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    class mViolationAdapter extends BaseAdapter {

        private String title;
        private ImageView violationItemImg;
        private TextView violationItemTitle;
        private LinearLayout violationItemContent;

        public mViolationAdapter() {
        }

        public mViolationAdapter(String title) {
            this.title = title;
        }

        @Override
        public int getCount() {
            if (title.equals("违章图片"))
                return imgUrl.size();
            if (title.equals("违章视频"))
                return videoUrl.size();
            return 0;
        }

        @Override
        public Object getItem(int i) {
            if (title.equals("违章图片"))
                return imgUrl.get(i);
            if (title.equals("违章视频"))
                return videoUrl.get(i);
            return null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, viewGroup, false);
            initView(view);
            if (title.equals("违章图片")){
                ImageUtils.DownLoad(violationItemImg,imgUrl.get(i),queue);
                violationItemTitle.setText("违章图片"+(i+1));
                violationItemContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getContext(),ImageActivity.class);
                        intent.putExtra("title","违章图片"+(i+1));
                        intent.putExtra("url",imgUrl.get(i));
                        startActivity(intent);
                    }
                });
            }else if (title.equals("违章视频")){
                violationItemImg.setImageResource(R.drawable.ic_car_foreground);
                violationItemTitle.setText("违章视频"+(i+1));
                violationItemContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getContext(),VideoActivity.class);
                        intent.putExtra("title","违章视频"+(i+1));
                        intent.putExtra("url",videoUrl.get(i));
                        startActivity(intent);
                    }
                });
            }
            return view;
        }

        private void initView(View view) {
            violationItemImg = view.findViewById(R.id.violation_item_img);
            violationItemTitle = view.findViewById(R.id.violation_item_title);
            violationItemContent = view.findViewById(R.id.violation_item_content);
        }
    }
}
